<!-- 素材库状态 -->
<template>
  <div class="materialLibrary">
    <div class="materialLibraryTitle">
      <div @click="materialClick(1)" :class=" materialState == 1 ? 'materialTitle active' : 'materialTitle' ">图片</div>
      <div @click="materialClick(2)" :class=" materialState == 2 ? 'materialTitle active' : 'materialTitle' ">视频</div>
    </div>
    <div class="materialContent">
      <div v-if="materialState == 1" class="contentClass">
        <div v-for="item in imageList" :key="item.id" class="scriptItem">
          <div class="titleClass" @click="imageClick(item)">
            <div class="title">{{ item.title }}</div>
            <div :class="imageIndex == item.id ? 'rotateTrue rotateFalse' : 'rotateTrue' ">
              <el-icon><ArrowRight /></el-icon>
            </div>
          </div>
          <div class="childrenClass" v-if="imageIndex == item.id">
            <div v-for="child in item.children" :key="child.id" class="childrenItem" @click="childrenImageClick(child)">
              <div class="imageClass">
                <img :src="child.imageUrl" class="image">
              </div>
              <div v-if="child.state" class="active">
                <el-icon size="20" color="rgb(77, 217, 131)"><CircleCheckFilled /></el-icon>
              </div>
              <div v-else class="masking"></div>
            </div>
          </div>
        </div>
      </div>
      <div v-if="materialState == 2" class="contentClass">
        <div v-for="item in videoList" :key="item.id" class="scriptItem">
          <div class="titleClass" @click="videoClick(item)">
            <div class="title">{{ item.title }}</div>
            <div :class="videoIndex == item.id ? 'rotateTrue rotateFalse' : 'rotateTrue' ">
              <el-icon><ArrowRight /></el-icon>
            </div>
          </div>
          <div class="childrenClass" v-if="videoIndex == item.id">
            <div v-for="child in item.children" :key="child.id" class="childrenItem" @click="childrenVideoClick(child)">
              <div class="imageClass">
                <img :src="child.videoUrl" class="image">
              </div>
              <div v-if="child.state" class="active">
                <el-icon size="20" color="rgb(77, 217, 131)"><CircleCheckFilled /></el-icon>
              </div>
              <div v-else class="masking"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="materialBottom" v-show="selectImageList.length > 0">
      <div class="text">选中 {{ selectImageList.length }} 张素材</div>
      <div>
        <el-button type="info" @click="sendClick">发送</el-button>
      </div>
    </div>
  </div>
</template>


<script setup>
import { computed,ref,onMounted } from 'vue';
import avatar from  '@/assets/login/12.jpg'

let materialState = ref(1);
const materialClick = (type) => {
  materialState.value = type;
  if(type == 2){
    imageList.value.forEach(element => {
      element.children.forEach(ele => {
        ele.state = false
      })
    })
    selectImageList.value = []
  } else {
    videoList.value.forEach(element => {
      element.children.forEach(ele => {
        ele.state = false
      })
    })
    selectVideoList.value = []
  }
 
}

//#region
let imageList = ref([
  {
    title: "素材名称1",
    id: 1,
    children: [],
  },
  {
    title: "素材名称2",
    id: 2,
    children: [],
  },
])
let imageIndex = ref(null);
let selectImageList = ref([]);
const imageClick = (item) => {//打开素材列表
  if(imageIndex.value == null){
    imageIndex.value = item.id;
  } else {
    imageIndex.value = imageIndex.value == item.id ? null : item.id;
  }

  if(imageIndex.value){
    if(item.children.length == 0){
      item.children = [
        {
          imageUrl: avatar,
          id: 11,
          state: false,
        },
        {
          imageUrl: avatar,
          id: 12,
          state: false,
        }
      ]
    }
  }
}
const childrenImageClick = (item) => {//选择图片
  item.state = !item.state;
  if(item.state){
    selectImageList.value.push(item)
  } else {
    let i = selectImageList.value.findIndex(ele => {
      return ele.id == item.id
    })
    if(i != -1){
      selectImageList.value.splice(i,1)
    }
  }
}
const sendClick = () => {//发送
  console.log(selectImageList.value);
  imageList.value.forEach(element => {
    element.children.forEach(ele => {
      ele.state = false
    })
  })
  selectImageList.value = []
}
//#endregion

//#region
let videoList = ref([
  {
    title: "素材名称1",
    id: 1,
    children: [],
  },
  {
    title: "素材名称2",
    id: 2,
    children: [],
  },
])
let videoIndex = ref(null);
let selectVideoList = ref([]);
const videoClick = (item) => {//打开素材列表
  if(videoIndex.value == null){
    videoIndex.value = item.id;
  } else {
    videoIndex.value = videoIndex.value == item.id ? null : item.id;
  }

  if(videoIndex.value){
    if(item.children.length == 0){
      item.children = [
        {
          videoUrl: avatar,
          id: 11,
          state: false,
        },
        {
          videoUrl: avatar,
          id: 12,
          state: false,
        }
      ]
    }
  }
}
const childrenVideoClick = (item) => {//选择视频
  item.state = !item.state;
  if(item.state){
    selectVideoList.value.push(item)
  } else {
    let i = selectVideoList.value.findIndex(ele => {
      return ele.id == item.id
    })
    if(i != -1){
      selectVideoList.value.splice(i,1)
    }
  }
}
//#endregion

</script>


<style lang="scss" scoped>
.materialLibrary{
  width: 100%;
  height: 100%;
  background-color: #fff;
  
  .materialLibraryTitle{
    width: 100%;
    display: flex;
    .materialTitle{
      width: 50%;
      font-weight: 400;
      font-size: 16px;
      color: rgb(16, 16, 16);
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 65px;
    }
    .active{
      color: #fff;
      background: rgb(165, 211, 255);
    }
  }
  .materialContent{
    height: calc( 100% - 117px);
    padding: 20px 10px;
    overflow-y: auto;
    .contentClass{
      .scriptItem{
        .titleClass{
          display: flex;
          justify-content: space-between;
          border-radius: 4px;
          padding: 10px 10px;
          margin-top: 10px;
          cursor: pointer;
          background-color: #f1f3ff;
          .title{
            font-weight: 400;
            font-size: 14px;
            color: rgb(16, 16, 16);
          }
          .rotateTrue{
            transition:  0.5s;
            transform: rotate(0);
          }
          .rotateFalse{
            transform: rotate(90deg);
          }
        }
        .childrenClass {
          display: flex;
          flex-wrap: wrap;
          width: 100%;
          .childrenItem{
            width: 50%;
            padding: 5px;
            position: relative;
            cursor: pointer;
            .imageClass{
              width: 100%;
              .image{
                width: 100%;
                aspect-ratio: 1/1;
                display: block;
                object-fit: cover;
              } 
            }
            .masking{
              position: absolute;
              bottom: 10px;
              right: 11px;
              width: 17px;
              height: 17px;
              border-radius: 50%;
              border: 1px solid #fff;
            }
            .active{
              position: absolute;
              bottom: 5px;
              right: 10px;
            }
          }
          
        }
      }
    }
  }
  .materialBottom{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 10px;
    .text{
      font-weight: 400;
      font-size: 14px;
      color: rgb(16, 16, 16);
    }
  }
}
</style>